package org.yang.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yang.reggie.common.R;
import org.yang.reggie.entity.Orders;
import org.yang.reggie.service.OrderDetailService;
import org.yang.reggie.service.OrderService;

/**
 * @author 缘友一世
 * date 2023/7/13-13:08
 * 订单
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 查询订单明细
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> list(int page, int pageSize) {
        log.info("page={},pageSize={}",page,pageSize);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        queryWrapper.orderByAsc(Orders::getId);
        //执行查询 无需接收返回值，直接封装到了pageInfo中
        orderService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /* @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize) {
        log.info("page={},pageSize={}",page,pageSize);
        //构造分页构造器
        Page<Orders> pageInfo = new Page(page, pageSize);
        Page<OrdersDto> pageDtoInfo = new Page(page, pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        queryWrapper.orderByAsc(Orders::getId);

        orderService.page(pageInfo,queryWrapper);

        //对象拷贝 由于泛型类型（实体类不一样），所以需要忽略records，手动拷贝
        BeanUtils.copyProperties(pageInfo,pageDtoInfo,"records");

        List<OrdersDto> ordersDtos=null;

        List<Orders> records = pageInfo.getRecords();
        //item为遍历出来的每一个Orders实体
        ordersDtos=records.stream().map((item)->{
            //新建需要封装的ordersDto 然后再赋值
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);
            //根据id分别查询
            Long id = item.getId();

            orderDetailService.
        }).collect(Collectors.toList());


        return R.success(pageInfo);
    } */
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize) {
        log.info("page={},pageSize={}",page,pageSize);
        //构造分页构造器
        Page<Orders> pageInfo = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        queryWrapper.orderByAsc(Orders::getId);
        //执行查询 无需接收返回值，直接封装到了pageInfo中
        orderService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
}
